<?php
namespace app\common\model\mysql;

use think\Model;
use think\Db;

/**
 * @author : SN
 * @time   : 2020/3/30 16:09
 * @des    :
 */
class BaseModel extends Model
{

    //开启自动时间戳
    protected $autoWriteTimestamp = true;

    /**
     * Create by SN on 2020/2/24 12:33
     * Des : 分页列表查询
     * @param $page_index 第几页
     * @param $page_size 每页多少条
     * @param $condition 查询条件
     * @param $order 排序规则
     * @param $field 要查询的字段
     */
    public function pageQuery($page_index, $page_size, $condition, $order, $field)
    {
        $order = trim($order);
        //总条数
        $count = $this->where($condition)->count();
        if ($page_size == 0) {//不分页
            $list = $this->field($field)
                ->where($condition)
                ->order($order)
                ->select();
            $page_count = 1;
        } else {//分页
            $start_row = $page_size * ($page_index - 1);
            $list = $this->field($field)
                ->where($condition)
                ->order($order)
                ->limit($start_row . "," . $page_size)
                ->select();
            if ($count % $page_size == 0) {
                $page_count = $count / $page_size;
            } else {
                $page_count = (int)($count / $page_size) + 1;
            }
        }
        return array(
            'data' => $list,
            'total_count' => $count,
            'page_count' => $page_count
        );
    }


    /**
     * Create by SN on 2020/2/24 12:40
     * Des : 条件查询
     * @param $condition     查询条件
     * @param string $field 要查询的字段
     * @param string $order 排序规则
     * @param string $group 分组查询
     */
    public function getQuery($condition = null, $field = '*', $order = '', $group = '')
    {
        $order = trim($order);
        if (empty($group)) {
            $list = $this->field($field)->where($condition)->order($order)->select();
        } else {
            $list = $this->field($field)->where($condition)->group($group)->order($order)->select();
        }
        return $list;
    }

    /**
     * Create by SN on 2020/2/24 12:44
     * Des : 查询单条信息
     * @param string $condition 查询条件
     * @param string $field 查询的字段, *代表查询所有
     * @return
     */
    public function getInfo($condition = '', $field = '*')
    {
        $info = Db::table($this->table)->where($condition)
            ->field($field)
            ->find();
        return $info;
    }

    /**
     * Create by SN on 2020/2/24 12:48
     * Des :  查询第一条数据
     * @param $condition
     * @param $order
     */
    public function getFirstData($condition, $order)
    {
        $data = Db::table($this->table)->where($condition)->order($order)
            ->limit(1)->select();
        if (!empty($data)) {
            return $data[0];
        } else
            return '';
    }

    /**
     * Create by SN on 2020/2/24 12:46
     * Des : 查询数据的数量
     * @param $condition
     * @return float|string
     */
    public function getCount($condition)
    {
        $count = Db::table($this->table)->where($condition)
            ->count();
        return $count;
    }

    /**
     * Create by SN on 2020/2/24 12:49
     * Des : sum查询
     * @param $condition
     * @param $field
     * @return float|int
     */
    public function getSum($condition, $field)
    {
        $sum = Db::table($this->table)->where($condition)
            ->sum($field);
        if (empty($sum)) {
            return 0;
        } else
            return $sum;
    }

    /**
     * Create by SN on 2020/2/24 12:50
     * Des : 查询数据最大值
     * @param $condition
     * @param $field
     * @return int|mixed
     */
    public function getMax($condition, $field)
    {
        $max = Db::table($this->table)->where($condition)
            ->max($field);
        if (empty($max)) {
            return 0;
        } else
            return $max;
    }

    /**
     * Create by SN on 2020/2/24 12:50
     * Des : 查询数据最小值
     * @param $condition
     * @param $field
     * @return int|mixed
     */
    public function getMin($condition, $field)
    {
        $min = Db::table($this->table)->where($condition)
            ->min($field);
        if (empty($min)) {
            return 0;
        } else
            return $min;
    }

    /**
     * Create by SN on 2020/2/24 12:50
     * Des : 查询数据均值
     * @param $condition
     * @param $field
     * @return float|int
     */
    public function getAvg($condition, $field)
    {
        $avg = Db::table($this->table)->where($condition)
            ->avg($field);
        if (empty($avg)) {
            return 0;
        } else
            return $avg;
    }


    //添加 -- 一条数据
    public function dataAdd($information)
    {
        $insert = Db::table($this->table)->insert($information);
        return $insert;
    }

    //添加 -- 一条数据并返回id
    public function dataAddGetId($information)
    {
        $insertGetId = Db::table($this->table)->insertGetId($information);
        return $insertGetId;
    }

    //添加 -- 多条数据
    public function dataAddAll($information)
    {
        $insertAll = Db::table($this->table)->insertAll($information);
        return $insertAll;
    }

    //更新 -- 数据
    public function dataUpdate($condition, $update_data)
    {
        $update = Db::table($this->table)->where($condition)->update($update_data);
        return $update;
    }


    //删除 -- 数据
    public function dataDel($id)
    {
        $delete = Db::table($this->table)->where(['id' => $id])->delete();
        return $delete;
    }

    //软删除 -- 数据
    public function dataDelSoft($id)
    {
        $delete = Db::table($this->table)->where(['id' => $id])->update(['is_del' => 1]);
        return $delete;
    }


    /**
     * Create by SN on 2020/2/24 16:26
     * Des : 连表查询
     * @param $viewObj
     * @param $page_index
     * @param $page_size
     * @param $condition
     * @param $order
     * @return mixed
     */
    public function viewPageQuery($viewObj, $page_index, $page_size, $condition, $order)
    {
        if ($page_size == 0) {
            $list = $viewObj->where($condition)
                ->order($order)
                ->select();
        } else {
            $start_row = $page_size * ($page_index - 1);
            $list = $viewObj->where($condition)
                ->order($order)
                ->limit($start_row . "," . $page_size)
                ->select();
        }
        return $list;
    }

    /**
     * Create by SN on 2020/2/24 16:29
     * Des : 连表查询数据
     * @param $viewObj
     * @param $condition
     * @return mixed
     */
    public function viewCount($viewObj, $condition)
    {
        $count = $viewObj->where($condition)->count();
        return $count;
    }


    /**
     * 数据库开启事务
     */
    public function startTrans()
    {
        Db::startTrans();
    }

    /**
     * 数据库事务提交
     */
    public function commit()
    {
        Db::commit();
    }

    /**
     * 数据库事务回滚
     */
    public function rollback()
    {
        Db::rollback();
    }


}